package netty.tcp.tasker;

import io.netty.util.Timeout;
import io.netty.util.TimerTask;
import netty.tcp.websocket.Session;

import java.util.concurrent.atomic.AtomicBoolean;

public class TaskerMessageSender implements TimerTask {

    private final Session session;

    private final AtomicBoolean inTimeWheel = new AtomicBoolean(false);

    public TaskerMessageSender(Session session) {
        this.session = session;
    }

    public Session getSession() {
        return session;
    }

    @Override
    public void run(Timeout timeout) throws Exception {
        session.flush();
        inTimeWheel.set(false);
    }

    public void trigger() {
        if (inTimeWheel.compareAndSet(false, true)) {
            TaskerManager.instance().trigger(this);
        }
    }
}
